package com.itheima.dianming.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dianming.aop.HasAuth;
import com.itheima.dianming.pojo.domain.Project;
import com.itheima.dianming.pojo.domain.Stu;
import com.itheima.dianming.pojo.domain.StuProject;
import com.itheima.dianming.pojo.vo.R;
import com.itheima.dianming.service.ProjectService;
import com.itheima.dianming.service.StuProjectService;
import com.itheima.dianming.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/proj")
@Slf4j
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private StuProjectService stuProjectService;

    @Autowired
    private StuService stuService;

    /**
     * 初始化学生对应项目数据
     * @param projectId 项目id
     * @return
     */
    @PostMapping("/load")
    @HasAuth("ROLE_ADMIN")
    public R loadData(Long projectId){
        List<Stu> list = stuService.list();
        LambdaQueryWrapper<StuProject> qw = new LambdaQueryWrapper<>();
        qw.eq(StuProject::getProjectId,projectId);
        stuProjectService.remove(qw);
        log.info("清除项目数据{}",projectId);
        for (Stu stu : list) {
            StuProject stuProject = new StuProject();
            stuProject.setStuId(stu.getId());
            stuProject.setProjectId(projectId);
            stuProjectService.save(stuProject);
        }
        return R.onflag(true,"加载数据成功");
    }


    @GetMapping
    public R queryPage(Integer page,Integer pageSize,String name){
        IPage<Project> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Project> qw = new LambdaQueryWrapper<>();
        if(!StrUtil.isBlank(name)&&!"undefined".equals(name)){
            qw.like(Project::getProjectName,name);
        }
        projectService.page(pageInfo,qw);
        Map map = new HashMap();
        map.put("records",pageInfo.getRecords());
        map.put("total",pageInfo.getTotal());
        return R.data(map);
    }

    @PostMapping
    @HasAuth("ROLE_ADMIN")
    public R save(@RequestBody Project project){
        boolean save = projectService.save(project);
        return R.onflag(save,save?"":"操作失败！");
    }

    @PutMapping
    @HasAuth("ROLE_ADMIN")
    public R update(@RequestBody Project project){
        boolean save = projectService.updateById(project);
        return R.onflag(save,save?"":"操作失败！");
    }

    @DeleteMapping("/{id}")
    @HasAuth("ROLE_ADMIN")
    public R deleteById(@PathVariable Integer id){
        boolean b = projectService.removeById(id);
        return R.onflag(b);
    }

    @GetMapping("/list")
    public R list(){
        List<Project> list = projectService.list();
        return R.data(list);
    }
}
